package cn.youframe.sample.mail;

import cn.youframe.frame.base.BaseCtrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * User: Administrator
 * DateTime: 12-7-17 下午10:20
 * Desc:
 */
@Controller
@RequestMapping(value = "/sample")
public class MailSendCtrl extends BaseCtrl {
    @Autowired
    JavaMailSender mailSender;
    @RequestMapping(value = "/MailSendCtrl!sendMail.do")
    public String sendMail(HttpServletRequest request){
        String reciver=request.getParameter("reciver");
        String subject=request.getParameter("subject");
        String mailContent=request.getParameter("mailContent");
        String sendMsg;
        try{
            SimpleMailMessage mail = new SimpleMailMessage();
            mail.setFrom("wzg30@163.com");
            mail.setTo(reciver);
            logger.info("Send To:"+reciver);
            mail.setSubject(subject);
            mail.setText(mailContent);
            mailSender.send(mail);
            sendMsg="发送成功！";
        }catch (Exception e){
            sendMsg="发送失败："+e.getMessage();
            logger.error(e.getMessage(),e);
        }
        request.setAttribute("sendMsg",sendMsg);
        return "sample/mail/SendMail";
    }
}
